#!/bin/bash

#从各大区拉取实时数据
root_dir="/data/home/app100711417/tony/pull_data"

from_file="${root_dir}/zone.cfg"
to_file="${root_dir}/dst_db.cfg"

#汇总db的配置
to_ip=""
to_port=""
to_user=""
to_passwd=""
to_gameDB=""
to_ossDB=""
while read -r ip port user passwd gameDB ossDB x
do
    if [[ ${ip:0:1} == "#" ]] || [[ ${#ip} -lt 4 ]] ; then
        continue;
    fi
	
	to_ip=$ip  to_port=$port to_user=$user to_passwd=$passwd  to_ossDB=$ossDB to_gameDB=$gameDB
	echo "target info:" $to_ip $to_port $to_user $to_passwd $to_gameDB  
	break;
done < ${to_file}

tmp_file="${root_dir}/oss_tmp.dat"
while read zoneid ip port user passwd gameDB ossDB  x
do
	#过滤掉空行和注释行
	#echo $zoneid,  ${#zoneid}, $ip
	if [[ ${zoneid:0:1} == "#" ]] || [[ ${#ip} -lt 1 ]] ; then
		continue;
	fi
	update_time=`mysql -h ${to_ip} -P${to_port} -u${to_user} -p${to_passwd} -N -e "select max(UpdateDate) from ${to_gameDB}.TOssZoneData where ZoneID = ${zoneid}"`
    if [ "$update_time" == "NULL" ] ; then
       update_time="1970-01-01 00:00:00"
    fi

	echo "source info:" ${ip} ${port} ${gameDB}  "update data before time:" ${update_time} -@-
	
	mysqldump -h ${ip} -P ${port} -u${user} -p${passwd} --insert-ignore --no-create-info ${gameDB} TOssZoneData --where="UpdateDate > \"${update_time}\""  > ${tmp_file}	

	#导入数据
	mysql -h ${to_ip} -P ${to_port} -u${to_user} -p${to_passwd} ${to_gameDB} < ${tmp_file}	
done < $from_file
rm -f ${tmp_file}



